#include <iostream>
#include <vector>
#include <string>
#include <thread>
#include <sw/redis++/redis++.h>

using namespace std;
using namespace sw::redis;
using namespace std::chrono_literals;

void test1(Redis &redis)
{
    cout << "using zadd and zrange" << endl;
    long long ret = redis.zadd("key", {make_pair("f1", 10), make_pair("f2", 20), make_pair("f3", 30)});
    cout << ret << endl;

    vector<string> values;
    auto it = back_inserter(values);
    redis.zrange("key", 0, -1, it);
    for(auto& value : values)
        cout << value << endl;

    vector<pair<string, double>> key_values;
    auto it2 = back_inserter(key_values);
    redis.zrange("key", 0, -1, it2);

    for(auto& [key, value] : key_values)
        cout << key << " " << value << endl;
}

void test2(Redis &redis)
{
    cout << "using zcard" << endl;
    vector<pair<string, double>> values = 
    {
        make_pair("f1", 10), 
        make_pair("f2", 20), 
        make_pair("f3", 30)
    };
    redis.zadd("key", values.begin(), values.end());

    long long len = redis.zcard("key");
    cout << len << endl;
}

void test3(Redis &redis)
{
    cout << "using zrem and zscore and zrank" << endl;
    vector<pair<string, double>> values = 
    {
        make_pair("f1", 10), 
        make_pair("f2", 20), 
        make_pair("f3", 30)
    };
    redis.zadd("key", values.begin(), values.end());

    redis.zrem("key", "f2");
    long long len = redis.zcard("key");
    cout << len << endl;

    auto score = redis.zscore("key", "f3");
    if(score) cout << score.value() << endl;
}

void test4(Redis &redis)
{
    cout << "using zrank" << endl;
    vector<pair<string, double>> values = 
    {
        make_pair("f1", 10), 
        make_pair("f2", 20), 
        make_pair("f3", 30)
    };
    redis.zadd("key", values.begin(), values.end());

    auto rank = redis.zrank("key", "f2");
    if(rank) cout << "rank: " << rank.value() << endl;
}

int main()
{
    sw::redis::Redis redis("tcp://127.0.0.1:6379");
    // test1(redis);
    // test2(redis);
    // test3(redis);
    test4(redis);
    redis.flushall();
    return 0;
}